package com.example.crossborderecommerce.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crossborderecommerce.entity.*;
import com.example.crossborderecommerce.mapper.LogisticsProviderMapper;
import com.example.crossborderecommerce.mapper.LogisticsProviderOrderMapper;
import com.example.crossborderecommerce.service.ILogisticsProviderService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 物流商模块
 *
 * @author Yolanda
 * @since 2024-05-09
 */
@RestController
@Transactional
@RequestMapping("/logistics-provider")
public class LogisticsProviderController {
    @Resource
    ILogisticsProviderService logisticsProviderService;
    @Resource
    LogisticsProviderMapper logisticsProviderMapper;

    /**
     * 添加物流商
     * @param logisticsProvider
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody LogisticsProvider logisticsProvider){
        boolean res = logisticsProviderService.save(logisticsProvider);
        if(res){
            return Result.success("添加物流商成功！");
        }
        return Result.error("添加物流商失败！");
    }

    /**
     * 获取物流商总数
     * @return
     */
    @GetMapping("/count")
    public Result count() {
        long count = logisticsProviderService.count();
        return Result.success("获取物流商数量成功", count);
    }

    /**
     * 以列表形式返回所有的物流商
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<LogisticsProvider> logisticsProviderList = logisticsProviderService.list();
        if(logisticsProviderList == null) {
            return Result.error("获取全部物流商信息失败！");
        }
        return Result.success("获取全部物流商信息成功！", logisticsProviderList);
    }

    /**
     * 分页查询所有物流商信息
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize){
        QueryWrapper<LogisticsProvider> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        Page<LogisticsProvider> logisticsProviderPage = logisticsProviderService.page(new Page<>(pageNum,pageSize), queryWrapper);
        return Result.success("成功查询所有物流商！", logisticsProviderPage);
    }

    /**
     * 删除某个物流商
     * @param logisticsProvider
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody LogisticsProvider logisticsProvider){
        boolean res = logisticsProviderService.removeById(logisticsProvider);
        if (res){
            return Result.success("删除物流商成功！");
        }
        return Result.error("删除物流商失败！");
    }

    /**
     * 删除一系列物流商
     * @param ids
     * @return
     */
    @GetMapping("/deleteBatch")
    public Result deleteBatch(@RequestParam String ids) {
        List<String> logisticsProviderIds = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        boolean res = logisticsProviderService.removeByIds(logisticsProviderIds);
        if(res) {
            return Result.success("删除物流商成功！");
        }
        return Result.error("删除物流商失败！");
    }

    /**
     * 修改物流商
     * @param logisticsProvider
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody LogisticsProvider logisticsProvider) {
        boolean res = logisticsProviderService.saveOrUpdate(logisticsProvider);
        if (res) {
            return Result.success("修改物流商成功！");
        }
        return Result.error("修改物流商失败！");
    }

    /**
     * 导出物流商列表
     * @param name
     * @param response
     * @return
     */
    @GetMapping("exportLogisticsProviderList")
    public Result exportProductAttributeList(@RequestParam(defaultValue = "") String name,
                                             HttpServletResponse response) {
        List<LogisticsProvider> logisticsProviderList = logisticsProviderMapper.getLogisticsProvider(name);
        HSSFWorkbook wb = new HSSFWorkbook(); // 导出到excel文件中
        HSSFSheet sheet = wb.createSheet("表一");
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("ID");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("物流商名称");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("联系人");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("联系电话");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("支持配送区域");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("支持配送方式");
        cell.setCellStyle(style);
        for (int i = 0; i < logisticsProviderList.size(); i++) {
            row = sheet.createRow(i + 1);
            LogisticsProvider logisticsProvider = logisticsProviderList.get(i);
            row.createCell((short) 0).setCellValue(logisticsProvider.getId());
            row.createCell((short) 1).setCellValue(logisticsProvider.getName());
            row.createCell((short) 2).setCellValue(logisticsProvider.getContactName());
            row.createCell((short) 3).setCellValue(logisticsProvider.getContactPhone());
            row.createCell((short) 4).setCellValue(logisticsProvider.getSupportAddress());
            row.createCell((short) 5).setCellValue(logisticsProvider.getSupportChannel());
        }
        String fileName = "exportLogisticsProviderList.xls";
        // 用户下载
        try {
            ServletOutputStream outputStream = null;
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
            outputStream = response.getOutputStream();
            wb.write(outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.success("exportProductAttributeList");
    }

    /**
     * 获取在物流订单中的所有物流商
     * @return
     */
    @GetMapping("/getLogisticsProviderInOrder")
    public Result getLogisticsProviderInOrder() {
        List<LogisticsProvider> logisticsProviderList = logisticsProviderMapper.getLogisticsProviderInOrder();
        if(logisticsProviderList == null) {
            return Result.error("获取物流商失败！");
        }
        return Result.success(logisticsProviderList);
    }
}
